<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${classPath}.dao.${table.className}Dao">
	<resultMap id="${table.className}ResultMap" class="${classPath}.entity.${table.className}${entitySuffix}">
		<#list columns as column>
		<result column="${column.columnName}" property="${column.fieldName}" />
		</#list>
	</resultMap>
	
	<sql id="queryCondition">
		<dynamic prepend="where">
			<#list columns as column>
			<#if column.javaTypeName =="Timestamp">
			<isNotEmpty  property="${column.fieldName}Start" prepend="and">
				t.${column.columnName} &gt;= ${braceStart}${column.fieldName}Start${braceEnd} 
			</isNotEmpty>
			<isNotEmpty  property="${column.fieldName}End" prepend="and">
				t.${column.columnName} &lt;= ${braceStart}${column.fieldName}End${braceEnd} 
			</isNotEmpty>
			<#else>
			<isNotEmpty property="${column.fieldName}" prepend="and">
				t.${column.columnName}=${braceStart}${column.fieldName}${braceEnd}
			</isNotEmpty>
			</#if>
			</#list>
		</dynamic>
	</sql>
	
	<sql id="update_columns_all">
		<#list columns as column>
		<#if !column.isPrimary && column.isNullAble!="YES">
		<isNotEmpty property="${column.fieldName}">
		${column.columnName}=${braceStart}${column.fieldName},jdbcType=${column.typeName}${braceEnd}<#if column_has_next>,</#if>
		</isNotEmpty>
		<#elseif !column.isPrimary>
		${column.columnName}=${braceStart}${column.fieldName},jdbcType=${column.typeName}${braceEnd}<#if column_has_next>,</#if>
		</#if>
		</#list>
	</sql>
	
	<!--根据条件查询${table.remark}列表 -->
	<select id="select${table.className}List" parameterClass="${classPath}.entity.${table.className}Param" resultMap="${table.className}ResultMap">
		SELECT
		<#list columns as column>
		t.${column.columnName}<#if column_has_next>,</#if>
		</#list>
		from ${table.tableName} t
		<include refid="queryCondition" />
		<isEmpty property="orderField">
			order by t.create_time desc 
		</isEmpty>
		<isNotEmpty property="orderField">
			<isEmpty property="orderDirection">
			order by t.create_time desc 
			</isEmpty>
			<isNotEmpty property="orderDirection">
			order by ${braceStart}orderField${braceEnd} ${braceStart}orderDirection${braceEnd} 
			</isNotEmpty>
		</isNotEmpty>
		limit ${braceStart}startNum${braceEnd},${braceStart}pageSize${braceEnd}
	</select>
	
	<!--根据条件查询${table.remark}数量 -->
	<select id="select${table.className}List_count" parameterClass="${classPath}.entity.${table.className}Param" resultClass="java.lang.Integer">
		select count(1) from ${table.tableName} t
		<include refid="queryCondition" />	
	</select>
	
	<!--根据主键查询${table.remark}信息 -->
	<select id="select${table.className}ById" parameterClass="java.lang.String" resultMap="${table.className}ResultMap">
		SELECT
		<#list columns as column>
		t.${column.columnName}<#if column_has_next>,</#if>
		</#list>
		from ${table.tableName} t
		<dynamic prepend="where">
			<#list columns as column>
			<#if column.isPrimary>
			   	${column.columnName}=${braceStart}${column.fieldName}${braceEnd} <#if column_has_next>and</#if>
			</#if>
			</#list>
		</dynamic>
	</select>
	
	<!-- 更新${table.remark}信息 -->
	<insert id="insert${table.className}" parameterClass="${classPath}.entity.${table.className}${entitySuffix}">
		insert into ${table.tableName} (
		<#list columns as column>
		${column.columnName}<#if column_has_next>,</#if>
		</#list>
		) VALUES (
		<#list columns as column>
		${braceStart}${column.fieldName}${braceEnd}<#if column_has_next>,</#if>
		</#list>
		)
	</insert>
	
	<!-- 更新${table.remark}信息 -->
	<update id="update${table.className}ById" parameterClass="${classPath}.entity.${table.className}${entitySuffix}">
		update ${table.tableName} 
		<set>
			<#list columns as column>
			<#if !column.isPrimary>
			<isNotEmpty property="${column.fieldName}">
			${column.columnName}=${braceStart}${column.fieldName},jdbcType=${column.typeName}${braceEnd}<#if column_has_next>,</#if>
			</isNotEmpty>
			</#if>
			</#list>
		</set>
		<dynamic prepend="where">
			<#list columns as column>
			<#if column.isPrimary>
			  and ${column.columnName}=${braceStart}${column.fieldName}${braceEnd}
			</#if>
			</#list>
		</dynamic>
	</update>
	
	<!-- 根据ID删除${table.remark}信息 -->
	<delete id="delete${table.className}ById" parameterClass="java.lang.String">
		delete from ${table.tableName} 
		<dynamic prepend="where">
			<#list columns as column>
			<#if column.isPrimary>
			  and ${column.columnName}=${braceStart}${column.fieldName}${braceEnd}
			</#if>
			</#list>
		</dynamic>
	</delete>
</mapper>